package krypto.blindsignature.endpoint;

import krypto.blindsignature.dto.Key;
import krypto.blindsignature.message.GetPublicKeyRequest;
import krypto.blindsignature.message.GetPublicKeyResponse;
import krypto.blindsignature.message.SignMessageRequest;
import krypto.blindsignature.message.SignMessageResponse;
import krypto.blindsignature.service.SigningService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

/**
 * 
 * Endpoint web-serwice dostarczający funkcjonalności:
 * <ul>
 * <li>{@link getPublicKey}
 * <li>{@link signMessage}
 * </ul>
 */
@Endpoint
public class SigningServiceEndpoint {

	private static final Logger log = Logger.getLogger(SigningServiceEndpoint.class);
	
	@Autowired
	SigningService service;

	/**
	 * Funkcjonalność udostępniająca wysłanie klucza publicznego
	 * @see GetPublicKeyRequest
	 * @see GetPublicKeyResponse
	 */
	@PayloadRoot(namespace ="http://ics.p.lodz.pl/krypto/schema/SignService", localPart = "getPublicKeyRequest")
	public GetPublicKeyResponse getPublicKey(GetPublicKeyRequest request) {
		
		log.info("service: " + service);
		Key key = service.getPublicKey();
		
		GetPublicKeyResponse response = new GetPublicKeyResponse();
		response.setPublicKey(key.getKey());
		response.setN(key.getN());
		return response;
	}
	
	
	/**
	 * Funkcjonalność udostępniająca podpisanie wiadmości
	 * @see SignMessageRequest
	 * @see SignMessageResponse
	 */
	@PayloadRoot(namespace ="http://ics.p.lodz.pl/krypto/schema/SignService", localPart = "signMessageRequest")
	public SignMessageResponse signMessage(SignMessageRequest request)
	{
		SignMessageResponse response = new SignMessageResponse();
		response.setSign(service.sign(request.getMessage()));
		return response;
	}
	

}
